﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Web.Script.Services;

/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {

    public AutoComplete () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<string> AutoCompleteAjaxRequest(string prefix) {
        List<string> nameRecommend = new List<string>();
        DataTable dt = new DataTable();
        dt = GetDataWithPrefix(prefix);
        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                nameRecommend.Add(dt.Rows[i]["name"].ToString());
            }
        }
        return nameRecommend;
    }

    public DataTable GetDataWithPrefix(string prefix)
    {
        DataTable dt = new DataTable();
        class_database db = new class_database();
        string sql = "SELECT * FROM tbl_user WHERE name LIKE '%" + prefix + "%'";
        dt = db.GetDataTable(sql);
        return dt;
    }
    
}
